lobsterh5.main({
    data: {
        currentTab: 0,
        patientlist: [],
        selectPatient: null,
        // filterIndex: 0,
        yetOrder: [],
        startDate: "",
        endDate: "",
        // currentbranch: {},//当前分院
    },
    pageload() {
        var self = this;
        // self.data.currentbranch = JSON.parse(lobsterh5.GetAppStorage("selectbranch")) || {};
        self.data.startDate = self.myGetDay(3);
        self.data.endDate = self.myGetDay(0);
        self.initdata();
        self.initevent();
    },
    getdata() {
        var self = this;
        if (self.data.selectPatient) {
            if (self.data.currentTab == 0) {
                self.getYetOrder();
            } else if (self.data.currentTab == 1) {
                self.getAlreadyOrder();
            }
        }
    },
    getAlreadyOrder() {
        var self = this;
        var data = {
            CardType: self.data.selectPatient.CardType,
            CardNum: self.data.selectPatient.CardNum,
            StarDate: self.data.startDate,
            EndDate: self.data.endDate
        };
        $(".weui-loadmore").show();
        $.showLoading();
        lobsterh5.GETAPI("tsselfpay.getpaidPatPres", data).then(res => {
            $(".weui-loadmore").hide();
            $.hideLoading();
            console.log(res);

            var items = res.Items;
            // items.forEach(item => {
            //     item.PersonFee = self.getRegexNumber(item.PersonFee);
            // })
            self.inittemp("tplalready", "div_alreadyorder", { list: items });

        }).catch(res => {
            $.toptip(res.msg, 'warning');
            $.hideLoading();
        })
    },
    getYetOrder() {
        var self = this;
        var data = {
            CardType: 1,
            CardNum: self.data.selectPatient.CardNum,
        };
        $(".weui-loadmore").show();
        $.showLoading();
        lobsterh5.GETAPI("tsselfpay.getUnpaidPatPres", data).then(res => {
            $(".weui-loadmore").hide();
            $.hideLoading();

            console.log(res);

            self.data.yetOrder = [];

            res.Items.forEach(item => {
                if (self.data.yetOrder.map(x => { return x.Regno; }).indexOf(item.Regno) == -1) {
                    var patregrecord = {
                        CardType: item.CardType,
                        CardNum: item.CardNum,
                        Regno: item.Regno,
                        PatName: item.PatName,
                        Repnos: [],
                        Fee: 0,
                        FeeItems: []
                    }
                    self.data.yetOrder.push(patregrecord);
                }
            })

            self.data.yetOrder.forEach(record => {
                res.Items.forEach(item => {
                    if (record.Regno == item.Regno) {
                        var feeitem = item;

                        record.Fee += feeitem.UnitPrice * feeitem.Quantity;
                        if (record.Repnos.indexOf(item.Repno) == -1) {
                            record.Repnos.push(item.Repno);
                        }
                        feeitem.Unit = (item.Unit == "" ? "次" : item.Unit);
                        record.FeeItems.push(feeitem);
                    }
                })
            })

            // self.data.yetOrder = res.Items;
            // self.data.yetOrder.forEach(item => {
            //     item.PersonFee = self.getRegexNumber(item.PersonFee);
            // })
            self.inittemp("tplyet", "div_yetorder", { list: self.data.yetOrder });

        }).catch(res => {
            $.toptip(res.msg, 'warning');
            $(".weui-loadmore").hide();
            $.hideLoading();
        })
    },
    getRegexNumber(data) {
        const regexp = /(?:\.0*|(\.\d+?)0+)$/
        return data.replace(regexp, '$1')
    },
    initevent() {
        var self = this;
        $(".weui-loadmore").hide();
        $('#t1').tab({
            defaultIndex: 0,
            activeClass: 'tab-green',
            onToggle: function (index) {
                console.log('index' + index);
                self.data.currentTab = index;
                if (index == 0) {
                    $("#div_alreadycontent").hide();
                    $("#div_yetorder").show();

                    $(".foot").show();
                } else if (index == 1) {
                    $("#div_alreadycontent").show();
                    $("#div_yetorder").hide();
                    $(".foot").hide();
                }
                self.getdata();
            }
        })

        //选定就诊人
        $(document).on("click", "#patiengdiv", function () {
            $("#half").popup();

            self.inittemp("tplcard", "sidebaritem", { list: self.data.patientlist });
        })

        //选定就诊人
        $(document).on("click", ".carditem", function (obj) {
            var card = obj.currentTarget.dataset.info;
            var info = JSON.parse(card);
            lobsterh5.GET("/admin/v1/CPatient/GetCardTypeListByPId", { patientId: info.PatientId }).then(res => {
                var cardList = res.cardList;//已绑卡
                var cardTypeList = res.cardTypeList;//卡类型字典
                //先找诊疗卡，未绑定诊疗卡则找身份证
                var arr = cardList.filter(x => x.Code == '1');
                if (arr.length > 0) {
                    info.CardType = 1;//诊疗卡
                    info.CardNum = arr[0].CardNo;
                } else {
                    info.CardType = 2;//身份证
                    info.CardNum = cardList.filter(x => x.Code == '2')[0].CardNo;
                }

                self.data.selectPatient = info;
                //测试就诊人 （身份证：320***********9739）
                $("#selectedPatient").html(info.Name + "  (" + info.IDCard + " )");
                $.closePopup()
                $("#div_data").html("");

                self.getdata();

            }).catch(res => {
                $.toast(res.msg, 'forbidden');
            });

        });

        //缴费
        $(document).on("click", ".toPay", function (obj) {
            var info = $(this).data("info");
            self.topay(info);
        });

        //已缴费 查看处方明细
        $(document).on("click", ".item", function (obj) {
            var info = $(this).data("info");
            location.href = "presdetail.html?appid=" + lobsterh5.GetAppId() + "&repno=" + info.Repno;
        });


        //已缴费 近三天 近一周 近一月 近一年
        $(document).on("click", ".label", function (obj) {
            var index = $(this).data("index");
            $(".label").removeClass("selectLable").addClass('f-gray');
            $(this).addClass('selectLable').removeClass('f-gray');
            self.filterdata(index);
        })
    },
    filterdata(index) {
        var self = this;
        let day = 3;
        switch (index) {
            case 0:
                day = 3;
                break;
            case 1:
                day = 7;
                break;
            case 2:
                day = 30;
                break;
            case 3:
                day = 90;
                break;
        }
        var end = self.formartdate(new Date());
        var start = self.myGetDay(day);

        self.data.startDate = start;
        self.data.endDate = end;
        console.log(end + '_' + start);
        self.getAlreadyOrder();
    },
    myGetDay: function (days) {
        var self = this;
        var time = (new Date).getTime() - 24 * 60 * 60 * 1000 * days;
        var date = new Date(time); // 获取的是前几天日期
        date = self.formartdate(date);
        return date;
    },
    formartdate(date) {
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? '0' + m : m;
        var d = date.getDate();
        d = d < 10 ? '0' + d : d;
        return y + '-' + m + '-' + d;
    },
    topay(data) {
        console.log(data);
        $.showLoading();

        lobsterh5.POSTAPI('tsselfpay.balance', data).then(retdata => {

            $.hideLoading();
            $.toast('打开收银台');

            setTimeout(function () {
                location.href = retdata.url;
            }, 1000)

        }).catch(res => {
            $.toast(res.msg, 'forbidden');
        })
    },
    initdata() {
        var self = this;
        lobsterh5.GET("/admin/v1/CPatient/GetUserPatient").then(res => {
            self.data.patientlist = res.patdata;
        }).catch(res => {
            $.toast(res.msg, 'text');
        });
    },

    inittemp(tempid, _id, data) {
        var html = template(tempid, data);
        $("#" + _id).html(html);
    },
})  